package com.leetcode;

/**
 * @author fy
 * @date 2022/3/29 11:52
 */
public class Solution91 {

    /**
     * 91. 解码方法
     *
     * @param s
     * @return
     */
    public int numDecodings(String s) {
        if (s == null) {
            return 0;
        }
        int n = s.length();
        int[] memo = new int[n + 1];
        memo[0] = 1;
        for (int i = 1; i <= n; i++) {
            char c2 = s.charAt(i - 1);
            if (c2 != '0' && memo[i - 1] != 0) {
                memo[i] += memo[i - 1];
            }
            if (i > 1) {
                char c1 = s.charAt(i - 2);
                int twoCode = (c1 - '0') * 10 + (c2 - '0');
                if (c1 != '0' && memo[i - 2] != 0 &&  twoCode <= 26) {
                    memo[i] += memo[i - 2];
                }
            }
        }

        return memo[n];
    }

    public static void main(String[] args) {
        System.out.println(new Solution91().numDecodings("121010"));
    }

}
